package com.hxkcloud.patient.photo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;

import com.hxkcloud.patient.config.ApplicationConfig;

import java.io.File;

/**
 * 拍照或相册管理类
 * @author meeno.tom
 *
 */
public class PhotoManager
{
	//相册时forresult的参数
	public static final int REQUEST_CODE_PICK_IMAGE = 101;
	//拍照
	public static final int REQUEST_CODE_CAPTURE_CAMEIA = 102;
	//相册（需要截图）
	public static final int REQUEST_CODE_IMAGE_CROP = 103;
	public static final int REQUEST_CODE_CAPTURE_CAMERA_CROP = 104;
	
	public static final int PHOTO_REQUEST_CUT = 105;
	//图片保存的地址
	public static String path;
	/**
	 * 相册,非截图
	 * @param activity
	 */
	public static void ablum(Activity activity)
	{
		Intent intent = null;
		if (Build.VERSION.SDK_INT < 19)
		{
			intent = new Intent(Intent.ACTION_GET_CONTENT, null);
		}
		else 
		{
			intent = new Intent(
					Intent.ACTION_PICK,
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		}
		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
		activity.startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
	}
	/**
	 * 相册,非截图
	 * @param activity
	 */
	public static void ablum(Fragment activity)
	{
		Intent intent = null;
		if (Build.VERSION.SDK_INT < 19)
		{
			intent = new Intent(Intent.ACTION_GET_CONTENT, null);
		}
		else 
		{
			intent = new Intent(
					Intent.ACTION_PICK,
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		}
		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
		activity.startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
	}
	/**
	 * 拍照,非截图
	 * @param activity
	 */
	public static void carema(Activity activity)
	{
		path = ApplicationConfig.PicAndVideoUrl + File.separator + System.currentTimeMillis() + ".png";
		File file = new File(path);
		if (!file.exists()) {
			File vDirPath = file.getParentFile();
			vDirPath.mkdirs();
		}
		Uri imageUri = Uri.fromFile(file);
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 调用照相机
		intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
		intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
		activity.startActivityForResult(intent, REQUEST_CODE_CAPTURE_CAMEIA);
	}
//	/**
//	 * 有些机型拍照时得不到uri
//	 */
//	public static void cameraWithUri()
//	{
//		
//	}
	/**
	 * 拍照,非截图
	 * @param activity
	 */
	public static void carema(Fragment activity)
	{
		path = ApplicationConfig.PicAndVideoUrl + File.separator + System.currentTimeMillis() + ".png";
		File file = new File(path);
		if (!file.exists()) {
			File vDirPath = file.getParentFile();
			vDirPath.mkdirs();
		}
		Uri imageUri = Uri.fromFile(file);
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 调用照相机
		intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
		intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
		activity.startActivityForResult(intent, REQUEST_CODE_CAPTURE_CAMEIA);
	}
	/**
	 * 取得图片文件路径
	 * @param requestCode
	 * @param resultCode
	 * @param data
	 * @param context
	 * @return
	 */
	public static String onActivityResult(int requestCode,
										  int resultCode, Intent data, Context context)
	{
		String returnPath = "";
		if (resultCode == Activity.RESULT_OK)
		{
			//相册
			if (requestCode == REQUEST_CODE_PICK_IMAGE)
			{
				Uri uri = data.getData();
				returnPath = UriUtils.getPath(context, uri);
			}
			//拍照
			else if (requestCode == REQUEST_CODE_CAPTURE_CAMEIA)
			{
				returnPath = path;
				path = "";
			}
		}
		return returnPath;
	}
	/**
	 * 打开相册
	 * 
	 * @param context
	 *            是个Activity
	 * @param size
	 *            裁剪的图片获取的大小，0表示没有使用固定大小裁剪
	 */
	public static void openAlbum(Activity context, int size)
	{
		path = ApplicationConfig.PicAndVideoUrl + File.separator + System.currentTimeMillis() + ".png";
		File file = new File(path);
		if (!file.exists()) {
			File vDirPath = file.getParentFile();
			if (!vDirPath.exists()) {
				vDirPath.mkdirs();
			}
		}
		Uri imageUri = Uri.fromFile(file);
		
		Intent intent ;
		if (Build.VERSION.SDK_INT<19)
		{
			intent = new Intent(Intent.ACTION_GET_CONTENT, null);
		    intent.setType("image/*");
		}
		else
		{
			intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
		}
		
		//intent.setType("image/*");
		
		intent.putExtra("crop", "true");
		if (size != 0) {
			intent.putExtra("aspectX", 1);
			intent.putExtra("aspectY", 1);
			intent.putExtra("outputX", size);
			intent.putExtra("outputY", size);
		}

		intent.putExtra("scale", true);
		intent.putExtra("return-data", false);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
		intent.putExtra("outputFormat",
				Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true);

		context.startActivityForResult(intent, PHOTO_REQUEST_CUT);
	}
	/**
	 * 打开相册
	 * 
	 * @param context
	 *            是个Activity
	 * @param size
	 *            裁剪的图片获取的大小，0表示没有使用固定大小裁剪
	 */
	public static void openAlbum(Fragment context, int size)
	{
		path = ApplicationConfig.PicAndVideoUrl + File.separator + System.currentTimeMillis() + ".png";
		File file = new File(path);
		if (!file.exists()) {
			File vDirPath = file.getParentFile();
			if (!vDirPath.exists()) {
				vDirPath.mkdirs();
			}
		}
		Uri imageUri = Uri.fromFile(file);
		
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
		intent.setType("image/*");
		intent.putExtra("crop", "true");
		if (size != 0) {
			intent.putExtra("aspectX", 1);
			intent.putExtra("aspectY", 1);
			intent.putExtra("outputX", size);
			intent.putExtra("outputY", size);
		}

		intent.putExtra("scale", true);
		intent.putExtra("return-data", false);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
		intent.putExtra("outputFormat",
				Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true);

		context.startActivityForResult(intent, PHOTO_REQUEST_CUT);
	}
	/**
	 * 调用系统进入剪裁页面 Activity
	 * 
	 * @param context
	 *            是个Activity
	 * @param size
	 *            裁剪的图片获取的大小，0表示没有使用固定大小裁剪
	 */
	public static void startPhotoZoomCir(Activity context, int size, Uri emptyUri) {
		Uri uri = null;
		if (emptyUri != null) {
			uri = emptyUri;
			//new File(url);
			uri = Uri.fromFile(new File(UriUtils.getPath(context, uri)));
		}else 
		{
			if (!TextUtils.isEmpty(path))
			{
				File file = new File(path);
				uri = Uri.fromFile(file);
			}
		}
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");// 打开照相获得的图片
		// crop为true是设置在开启的intent中设置显示的view可以剪裁
		intent.putExtra("crop", "true");
		if (size != 0) {
			// aspectX aspectY 是宽高的比例
			intent.putExtra("aspectX", 1);
			intent.putExtra("aspectY", 1);
			// outputX,outputY 是剪裁图片的宽高
			intent.putExtra("outputX", size);
			intent.putExtra("outputY", size);
		}

		intent.putExtra("scale", true);
		intent.putExtra("return-data", false);// 不可设置为true设置为true将会卡死
		intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true); // no face detection
		context.startActivityForResult(intent, PHOTO_REQUEST_CUT);
	}
	public static String onActivityResultCrop(Activity activity, int requestCode, int resultCode, Intent data)
	{
		if (resultCode == Activity.RESULT_OK)
		{
			switch (requestCode) {
			case REQUEST_CODE_CAPTURE_CAMEIA:
				startPhotoZoomCir(activity, 512, null);
				break;
			case PHOTO_REQUEST_CUT:
				File file = new File(path);
				if (file.exists())
				{
					return path;
				}
				else
				{
					Uri uri = data.getData();
					if (uri!=null) {
						Log.d("拍照--》", uri.toString());
						//适应4.0之后的系统
						startPhotoZoomCir(activity, 512, uri);
					}
				}
				break;
			default:
				break;
			}
		}
		return "";
	}
	/**
	 * 调用系统进入剪裁页面 Activity
	 */
	public static void startPhotoZoomCir(Fragment activity, int size, Uri emptyUri) {
		Uri uri = null;
		if (emptyUri != null) {
			uri = emptyUri;
			uri = Uri.fromFile(new File(UriUtils.getPath(activity.getActivity(), uri)));
		}else 
		{
			if (!TextUtils.isEmpty(path))
			{
				File file = new File(path);
				uri = Uri.fromFile(file);
			}
		}
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");// 打开照相获得的图片
		// crop为true是设置在开启的intent中设置显示的view可以剪裁
		intent.putExtra("crop", "true");
		if (size != 0) {
			// aspectX aspectY 是宽高的比例
			intent.putExtra("aspectX", 1);
			intent.putExtra("aspectY", 1);
			// outputX,outputY 是剪裁图片的宽高
			intent.putExtra("outputX", size);
			intent.putExtra("outputY", size);
		}

		intent.putExtra("scale", true);
		intent.putExtra("return-data", false);// 不可设置为true设置为true将会卡死
		intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true); // no face detection
		activity.startActivityForResult(intent, PHOTO_REQUEST_CUT);
	}
	public static String onActivityResultCrop(Fragment activity, int requestCode, int resultCode, Intent data)
	{
		if (resultCode == Activity.RESULT_OK)
		{
			switch (requestCode) {
			case REQUEST_CODE_CAPTURE_CAMEIA:
				startPhotoZoomCir(activity, 512, null);
				break;
			case PHOTO_REQUEST_CUT:
				File file = new File(path);
				if (file.exists())
				{
					return path;
				}
				else
				{
					Uri uri = data.getData();
					if (uri!=null) {
						Log.d("拍照--》", uri.toString());
						//适应4.0之后的系统
						startPhotoZoomCir(activity, 512, uri);
					}
				}
				break;
			default:
				break;
			}
		}
		return "";
	}
}
